package mwa.cs.sm.ctrl;

import java.io.IOException;
import java.util.Map;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONObject;

import mwa.cs.dao.ReceivePositionDAO;
import mwa.cs.dao.SMErrorLogDAO;
import mwa.cs.dao.SitesDAO;
import mwa.cs.database.Database;
import mwa.cs.model.ReceivePosition;
import mwa.cs.util.JDate;

/**
 * Servlet implementation class CS11002Srvl
 */
public class CS11002Srvl extends HttpServlet implements Servlet {
	private static final long serialVersionUID = 1L;
	private int vnSiteId;
	private int vnUserId;
    private Database db;
 	private String vaUrlAddress;
 	private String vaAgentBrowser;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CS11002Srvl() {
        super();
    }
    public void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		vnSiteId = Integer.parseInt(request.getSession().getAttribute("site_id").toString());
		vnUserId = Integer.parseInt(request.getSession().getAttribute("user_id").toString());
    	vaUrlAddress = request.getParameter("url_address");
    	vaAgentBrowser = request.getParameter("agent_browser");
    	String vaProcessType = request.getParameter("process_type");
		if(vaProcessType.equals("checkData")){
			checkReceivePosition(request,response);
		}else if(vaProcessType.equals("getReceivePositionData")){
			getReseivePosition(request,response);
		}else if (vaProcessType.equals("insert")){
			addData(request,response);
		}else{
			updateData(request,response);
		}
    }
    
    public void updateData(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    	request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/JSON");
		this.db = new Database();
		//JDate jDate = new JDate();
		String vaNameEmp = request.getParameter("nameEmp");
		String vaPositionEmp = request.getParameter("positionEmp");
		String vaNameHead = request.getParameter("nameHead");
		String vaPosition = request.getParameter("positionHead");
		String vaDate = JDate.getDate();
		String vaTime = JDate.getTime();
		
		ReceivePosition voReceivePosition = new ReceivePosition();
		voReceivePosition.setVnSiteId(vnSiteId);
		voReceivePosition.setVaEmName(vaNameEmp);
		voReceivePosition.setVaEmPosition(vaPositionEmp);
		voReceivePosition.setVaHeadName(vaNameHead);
		voReceivePosition.setVaHeadPosition(vaPositionEmp);
		voReceivePosition.setVnUpdateUserId(vnUserId);
		voReceivePosition.setVaUpdateDate(vaDate);
		voReceivePosition.setVaUpdateTime(vaTime);
		
		ReceivePositionDAO voReceivePositionDAO =new ReceivePositionDAO(db);
		try {
			db.beginTransaction();
			voReceivePositionDAO.updateReceivePosition(vnSiteId, voReceivePosition);
			db.commit();
		} catch (Exception e) {
			SMErrorLogDAO.getInstance().addLog(0, "CS11002", JDate.getDate(), JDate.getTime(),vnUserId, "CS11002Srvl", e.getMessage(),vaUrlAddress, vaAgentBrowser);
			e.getMessage();
			db.rollback();
		}finally{
			db.close();
		}
		response.getWriter().write("update_success");
    }
    public void addData(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    	request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/JSON");
		this.db = new Database();
		String vaNameEmp = request.getParameter("nameEmp");
		String vaPositionEmp = request.getParameter("positionEmp");
		String vaNameHead = request.getParameter("nameHead");
		String vaPosition = request.getParameter("positionHead");
		String vaDate = JDate.getDate();
		String vaTime = JDate.getTime();
		
		ReceivePosition voReceivePosition = new ReceivePosition();
		voReceivePosition.setVnSiteId(vnSiteId);
		voReceivePosition.setVaEmName(vaNameEmp);
		voReceivePosition.setVaEmPosition(vaPositionEmp);
		voReceivePosition.setVaHeadName(vaNameHead);
		voReceivePosition.setVaHeadPosition(vaPositionEmp);
		voReceivePosition.setVnInsertUserId(vnUserId);
		voReceivePosition.setVaInsertDate(vaDate);
		voReceivePosition.setVaInsertTime(vaTime);
		
		ReceivePositionDAO voReceivePositionDAO = new ReceivePositionDAO(db);
		try {
			db.beginTransaction();
			voReceivePositionDAO.addReceivePosition(voReceivePosition);
			db.commit();
		} catch (Exception e) {
			SMErrorLogDAO.getInstance().addLog(0, "CS11002", JDate.getDate(), JDate.getTime(),vnUserId, "CS11002Srvl", e.getMessage(),vaUrlAddress, vaAgentBrowser);
			db.rollback();
			e.getMessage();
		} finally{
			db.close();
		}
		response.getWriter().write("insert_success");
    }
    
    
    public void getReseivePosition(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    	request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/JSON");
		this.db = new Database();
		ReceivePositionDAO voReceivePositionDAO = new ReceivePositionDAO(db);
		Map<String, Object>  voMap = voReceivePositionDAO.findReceivePositonBySiteId(vnSiteId);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("emName",voMap.get("EMPLOYEE_NAME")==null?"":voMap.get("EMPLOYEE_NAME").toString());
		jsonObject.put("emPosition",voMap.get("EMPLOYEE_POSITION")==null?"":voMap.get("EMPLOYEE_POSITION").toString());
		jsonObject.put("nameHead",voMap.get("HEAD_NAME")==null?"":voMap.get("HEAD_NAME").toString());
		jsonObject.put("positionHead",voMap.get("HEAD_POSITION")==null?"":voMap.get("HEAD_POSITION").toString());
		
		db.close();
		response.getWriter().print(jsonObject.toJSONString());
    }
    
    public void checkReceivePosition(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    	request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/JSON");
		this.db = new Database();
		ReceivePositionDAO voReceivePositionDAO = new ReceivePositionDAO(db);
		 Map<String, Object>  voMap = voReceivePositionDAO.findReceivePositonBySiteId(vnSiteId);
		
		db.close();
		if(voMap == null){
			response.getWriter().write("no_value");
		}else{
			response.getWriter().write("success");
		}
    }
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request,response);
	}

}
